package campus.aiit.ssl.service;

import campus.aiit.ssl.dao.UserDao;
import com.iflytek.uaac.client.rest.context.UaacServiceContext;
import com.iflytek.uaac.common.domain.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author ihaokun
 * @date 2019/9/27 11:40
 */
@Service
public class IndexService {
  @Resource private UserDao userDao;

  public void logout(HttpServletRequest request, HttpServletResponse response) {
    request.getSession().invalidate();
  }

  public void login(HttpServletRequest req, HttpServletResponse resp,String[] url) throws IOException {
    String currentLoginName = UaacServiceContext.getUaacService().getCurrentLoginName(req.getSession());
    campus.aiit.ssl.domain.model.User userInfo = userDao.selectByPrimaryKey(currentLoginName);

    switch (userInfo.getRoleId()){
      case 0:
        resp.sendRedirect(url[0]);
        System.out.println("admin");
        break;
      case 1:
        resp.sendRedirect(url[1]);
        System.out.println("generalAdmin");
        break;
      case 3:
        resp.sendRedirect(url[2]);
        System.out.println("user");
        break;
      default:
        resp.setStatus(404);
        resp.getWriter().write("Login Failed, No this username");
    }
//    if (userDao.selectByPrimaryKey(currentLoginName) != null){
////      res p.sendRedirect(frontIndex + "?" + req.getHeader("Cookie"));
//      resp.sendRedirect(frontIndex);
//    } else {
//      resp.setStatus(404);
//      resp.getWriter().write("Login Failed, No this username");
//    }
  }

  public User userInfo(HttpSession session) {
    return UaacServiceContext.getUaacService().getCurrentUser(session);
  }
}